

#ifdef WX_PRECOMP //
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__


#include <wx/socket.h>
#include <wx/thread.h>


#include "tux.h"
#include "MsnDef.h"
#include "GlobalConfig.h"
#include "MainFrame.h"
#include "Util.h"

#include "../resource/xpm/logo_offline.xpm"

IMPLEMENT_APP(App);

DEFINE_EVENT_TYPE(EVT_DEBUG_RECEIVED);


//TODO use wxGetApp instead

MainFrame* App::frame;


App::App() : locale(NULL)
{
	frame = NULL;
}

App::~App()
{
	wxDELETE(locale);

	GlobalConfig::Destroy();

}

bool App::OnInit()
{
	FileUtil::MkDir(SystemUtil::GetUserDataDir());

	GlobalConfig::Initialize(SystemUtil::GetUserDataDir() + _T("/globals"));

	//TODO get config language
	int lang = GlobalConfig::GetLanguage();

	if(lang == -1){
		lang = wxLocale::GetSystemLanguage();

		if(lang = wxLANGUAGE_UNKNOWN){
			lang = wxLANGUAGE_ENGLISH;
		}
		GlobalConfig::SetLanguage(lang);
	}

	InitLanguage(lang);

	
    wxSocketBase::Initialize();


	//this will increase the exe size significantly
	//::wxInitAllImageHandlers();
	
	//wxImage::AddHandler(new wxPNGHandler);

	wxImage::AddHandler(new wxJPEGHandler);
	wxImage::AddHandler(new wxGIFHandler);
	wxImage::AddHandler(new wxBMPHandler);
	

	MsnDef::InitText();
	MsnDef::InitIconList();

    frame = new MainFrame(0L, _T("Tuxitty"), wxPoint(50,50), wxSize(800,600));

	frame->Show();

	return true;
}

MainFrame* App::GetMainFrame()
{
    return frame;
}

void App::InitLanguage(int lang)
{
	if(locale != NULL){
		delete locale;
	}
	locale = new wxLocale();

	//m_Locale.Init(wxLANGUAGE_DEFAULT, wxLOCALE_LOAD_DEFAULT | wxLOCALE_CONV_ENCODING);
	if(locale->Init(lang, wxLOCALE_CONV_ENCODING)){
		locale->AddCatalogLookupPathPrefix(_T("./resource/lang"));
		locale->AddCatalog(_T("tux"));
	}	

	GlobalConfig::SetLanguage(lang);
}

void App::RequestUserAttention()
{
	frame->RequestUserAttention();
}


void _tux_debug(const wxString& msg)
{
	//wxMutexLocker lock(debugMutex);
	wxString str = StringUtil::URLDecode(msg);
	wxString thread = wxString::Format(_T(" [%d] "), wxThread::GetCurrentId());
//	wxLogDebug(thread + str);


	if(App::GetMainFrame() != NULL){
		wxCommandEvent event(EVT_DEBUG_RECEIVED, -1);
		event.SetString(thread+str);

		App::GetMainFrame()->AddPendingEvent(event);
	}

/*
	wxFile dumpFile;
	wxString filePath = _T("dump");

    if(dumpFile.Open(filePath, wxFile::write_append)){
        dumpFile.Write(str);
		dumpFile.Write(_T("\r\n"));
		dumpFile.Flush();
    }
*/
}

void _tux_warn(const wxString& msg)
{
	wxLogWarning(StringUtil::URLDecode(msg));
/*
	wxString str = HttpUtil::URLDecode(msg);

	wxFile dumpFile;
	wxString filePath = _T("dump");

    if(dumpFile.Open(filePath, wxFile::write_append)){
        dumpFile.Write(str);
		dumpFile.Write(_T("\r\n"));
		dumpFile.Flush();
    }
	dumpFile.Close();
*/
}

void _tux_error(const wxString& msg)
{
	wxLogError(StringUtil::URLDecode(msg));
}